package com.yyge.mapper.impl;

import com.yyge.entity.AddrInfo;
import com.yyge.entity.Admin;
import com.yyge.mapper.AddrInfoMapper;
import com.yyge.mapper.AdminMapper;
import com.yyge.util.DruidDataSourceHandler;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

public class AdminMapperImpl implements AdminMapper {
    @Override
    public int insert(Admin admin) {
        // 得到连接池
        DataSource dataSource = DruidDataSourceHandler.getDataSource();
         // 基于QueryRunner类的对象来进行
        QueryRunner queryRunner=new QueryRunner(dataSource);
        String sql="insert into admin "+
                " (adminId, loginName, loginPwd, roleId, lastLoginTime, lastLoginIP,state)"+
                "values (?,?,?,?,?,?,?)";
        Object[] params={
                admin.getAdminId(),
                admin.getLoginName(),
                admin.getLogeinPwd(),
                admin.getRoleId(),
                admin.getLastLoginTime(),
                admin.getLastLoginIP(),
                admin.getState()
        };

        try {
           return queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteByPrimaryKey(Integer key) {
        String sql="delete from admin where adminId=?";
        DataSource dataSource=DruidDataSourceHandler.getDataSource();
        QueryRunner queryRunner=new QueryRunner(dataSource);
        try {
            return queryRunner.update(sql,key);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateByPrimaryKey(Admin admin) {
        String sql="update admin set "+
                "loginName=?,"+
                "loginPwd=?,"+
                "roleId=?,"+
                "lastLoginTime=?,"+
                "lastLoginIP=?,"+
                "state=?"+
           "where adminId=?";
        DataSource dataSource=DruidDataSourceHandler.getDataSource();
        QueryRunner queryRunner=new QueryRunner(dataSource);
        Object[] params= {
            admin.getLoginName(),
            admin.getLogeinPwd(),
            admin.getRoleId(),
            admin.getLastLoginTime(),
            admin.getLastLoginIP(),
            admin.getState(),
           admin.getAdminId()
        };
        try {
           return queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Admin selectByPrimaryKey(Integer key) {
        String sql="select * from admin where adminId=?";
        QueryRunner queryRunner=new QueryRunner(DruidDataSourceHandler.getDataSource());
        ResultSetHandler<Admin> rsh= new BeanHandler<>(Admin.class);
        try {
            Admin admin=queryRunner.query(sql,rsh,key);
            return admin;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
